#ifndef DBG_H
#define DBG_H

#include <cstdio>

static FILE * log;

#define LOG_FILE ""
#define DBG_PRINT(x) if(__DEBUG_TAB[x]) printf
#define LOG_PRINT(x, ...)  do { \
	if(__DEBUG_TAB[x] && log != NULL) \
		fprintf(log, __VA_ARGS__); \
	} while(0)

/*
 * @brief enumerator for log info flags
 */
enum LOG_INFO {
	LOG_DBG,
	LOG_START,
	LOG_SNAP,
	LOG_CRITIC,
	LOG_FULL,
	LOG_ROUTINE,
	LOG_ERROR,

	LOG_INFO_NUM
};

enum LOG_EXIT {
	LOG_NO_ERROR,
	LOG_OPEN_ERROR,
	LOG_NO_FILE_NAME,
	LOG_UNKNOWN_ERROR,

	LOG_EXIT_NUM
};

/* structures */

/* global var */

/*
 * @brief debug print flag table.
 * @function change according bool value to true if certain messages are desired to be printed.
 * @strict all entries in log info flag enumeration has to be presented. default all to false till manual change.
 */
const bool __DEBUG_TAB[LOG_INFO_NUM] = {
	false, // DBG
	false, // START
	false, // SNAP
	false, // CRITIC
	false, // FULL
	false, // ROUTINE
	true,  //ERROR
};

static inline void DBG_ENABLE(int logSel) {
	if (!__DEBUG_TAB[logSel])
		__DEBUG_TAB[logSel] == true;
}

static inline void DBG_DISABLE(int logSel) {
	if (__DEBUG_TAB[logSel])
		__DEBUG_TAB[logSel] == false;
}

static inline int LOG_INIT(char * log_name = NULL) {
	if (log_name == NULL)
		strcpy(log_name, LOG_FILE);

	if (!strcmp(log_name, "")) {
		log = fopen(LOG_FILE, "a+");
		if (log == NULL) {
			if (__DEBUG_TAB[LOG_ERROR]) {
				DBG_PRINT(LOG_ERROR) ("unable to open log file %s.\n", log_name);
			} else {
				DBG_ENABLE(LOG_ERROR);
				DBG_PRINT(LOG_ERROR) ("unable to open log file %s.\n", log_name);
				DBG_DISABLE(LOG_ERROR);
			}

			return LOG_OPEN_ERROR;
		}

		fprintf(log, "\n \
					 ************************************************************\n \
					 ************************************************************\n \
					 **  SecuraGeek Association All Rights Reserved.           **\n \
					 **                                                        **\n \
					 **  System Log                                            **\n \
					 **  %s %s\n** !!!Greeting!!!\n \
					 **                                                        **\n \
					 ************************************************************\n \
					 ************************************************************\n \
					 \n\n", __DATE__, __TIME__);

		return LOG_NO_ERROR;
	} else
		return LOG_NO_FILE_NAME;
}

#endif // DBG_H